package com.jhhc.StormSurgeForecast.StormSurge.Controller;

import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_TFPATH_F;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_TFPATH_R;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.TyphoonBO.ForecastPath;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.TyphoonBO.TyphoonCschemeInfo;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.TyphoonBO.RealPath;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.DTO.TyphoonParam;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.TyphoonBO.TyphoonInfo;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_CALCULATESCHEME_MService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_FBC_TFINFO_BService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_FBC_TFPATH_FService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_FBC_TFPATH_RService;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.Param.GetInfosParam;
import com.jhhc.utils.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LYQ
 * 台风信息
 */
@Api(value = "TyphoonController",tags = {"台风信息服务"})
@RestController
@RequestMapping(value = "StormSurgeForecast/Typhoon",headers = "Accept=application/json")
public class TyphoonController {

    @Resource(name = "BDMS_CALCULATESCHEME_MService")
    private BDMS_CALCULATESCHEME_MService mService;//计算方案表

    @Resource(name = "BDMS_FBC_TFPATH_RService")
    private BDMS_FBC_TFPATH_RService rService;//台风路径

    @Resource(name = "BDMS_FBC_TFPATH_FService")
    private BDMS_FBC_TFPATH_FService fService;

    @Resource(name = "BDMS_FBC_TFINFO_BService")
    private BDMS_FBC_TFINFO_BService bService;

    /**
     * 获取台风实际路径,
     * 1.先用台风id，去BDMS_FBC_TFPATH_R表查询所有的台风路径
     * 2.再用项目id，去BDMS_CALCULATESCHEME_M表查询完成的（state = 3）计算方案，,分割params字段，筛选出该台风id的方案
     * 3.循环1结果，并判断2结果里面是否具有相同的时间，相同hasScheme为true,不同为false
     */
    @RequestMapping(value = "/GetRealPaths", method = RequestMethod.POST)
    public JsonResult getRealPaths(@RequestBody TyphoonParam param){
        JsonResult jsonResult = new JsonResult();
        if (param.getTyphoonId() != null && !"".equals(param.getTyphoonId())) {
            List<BDMS_FBC_TFPATH_R> rList = rService.getRealPaths(param.getTyphoonId());
            if (rList == null || rList.size() == 0) {
                jsonResult.setRemark("获取台风实测路径失败，该台风id信息为空");
                return jsonResult;
            }else {
                List<RealPath> realPaths = new ArrayList<>();
                RealPath realPath = null;
                List<TyphoonCschemeInfo> typhoonParams = null;
                List<TyphoonCschemeInfo> typhoonParamList = new ArrayList<>();
                if (param.getProjectId() != null && !"".equals(param.getProjectId())) {
                    typhoonParams = mService.getRealPaths(Integer.parseInt(param.getProjectId()));
                    if (typhoonParams != null && typhoonParams.size() > 0) {
                        for (TyphoonCschemeInfo typhoonParam : typhoonParams) {
                            String[] strings = typhoonParam.getParams().split(",");
                            if (param.getTyphoonId().equals(strings[0])) {
                                typhoonParamList.add(typhoonParam);
                            }
                        }
                        typhoonParams = null;
                    }
                }
                for (BDMS_FBC_TFPATH_R bdmsFbcTfpathR : rList) {
                    realPath = new RealPath();
                    if (bdmsFbcTfpathR.getYmdhm() != null && !"".equals(bdmsFbcTfpathR.getYmdhm())) {
                        realPath.setRealTime(bdmsFbcTfpathR.getYmdhm());
                    }
                    if (bdmsFbcTfpathR.getLttd() != null) {
                        realPath.setLttd(bdmsFbcTfpathR.getLttd());
                    }
                    if (bdmsFbcTfpathR.getLgtd() != null) {
                        realPath.setLgtd(bdmsFbcTfpathR.getLgtd());
                    }
                    if (bdmsFbcTfpathR.getMaxspeed() != null) {
                        realPath.setMaxSpeed(bdmsFbcTfpathR.getMaxspeed());
                    }
                    if (bdmsFbcTfpathR.getMoveway() != null) {
                        realPath.setMoveWay(bdmsFbcTfpathR.getMoveway());
                    }
                    if (bdmsFbcTfpathR.getMovespeed() != null) {
                        realPath.setMoveSpeed(bdmsFbcTfpathR.getMovespeed());
                    }
                    if (bdmsFbcTfpathR.getPressure() != null) {
                        realPath.setPressure(bdmsFbcTfpathR.getPressure());
                    }
                    if (bdmsFbcTfpathR.getRadius7() != null && !"".equals(bdmsFbcTfpathR.getRadius7())) {
                        realPath.setRadius7(bdmsFbcTfpathR.getRadius7().split("\\|"));
                    } else {
                        realPath.setRadius7(new String[]{});
                    }
                    if (bdmsFbcTfpathR.getRadius10() != null && !"".equals(bdmsFbcTfpathR.getRadius10())) {
                        realPath.setRadius10(bdmsFbcTfpathR.getRadius10().split("\\|"));
                    } else {
                        realPath.setRadius10(new String[]{});
                    }
                    if (bdmsFbcTfpathR.getRadius12() != null && !"".equals(bdmsFbcTfpathR.getRadius12())) {
                        realPath.setRadius12(bdmsFbcTfpathR.getRadius12().split("\\|"));
                    } else {
                        realPath.setRadius12(new String[]{});
                    }
                    if (param.getProjectId() != null && !"".equals(param.getProjectId())) {
                        realPath.setHasArea(false);
                        realPath.setHasScheme(false);
                        if (typhoonParamList.size() > 0) {
                            List<TyphoonCschemeInfo> typhoons = typhoonParamList.stream().filter(typhoonParam ->
                                            typhoonParam.getDtmforecast().equals(bdmsFbcTfpathR.getYmdhm())).
                                    collect(Collectors.toList());
                            if (typhoons.size() > 0) {
                                realPath.setHasScheme(true);
                                if (typhoons.get(0).getIsarea() != null) {
                                    realPath.setHasArea(Integer.parseInt(typhoons.get(0).getIsarea()) == 2);
                                }
                            }
                        }
                    }
                    realPaths.add(realPath);
                }
                realPath = null;
                jsonResult.setData(realPaths);
                jsonResult.setRemark("获取台风实测路径信息成功");
            }
        }
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }

    /**
     * 获取台风路径
     */
    @RequestMapping(value = "/GetPaths", method = RequestMethod.POST)
    public JsonResult getPaths(@RequestBody TyphoonParam param){
        JsonResult jsonResult = new JsonResult();
        if (param.getTyphoonId() != null && !"".equals(param.getTyphoonId())) {
            List<BDMS_FBC_TFPATH_R> rList = rService.getRealPaths(param.getTyphoonId());
            List<BDMS_FBC_TFPATH_F> fList = fService.queryByTfidAll(param.getTyphoonId());
            if (rList == null || rList.size() == 0) {
                jsonResult.setRemark("获取台风实测路径失败，该台风id信息为空");
                return jsonResult;
            }else {
                List<RealPath> realPaths = new ArrayList<>();
                RealPath realPath = null;
                for (BDMS_FBC_TFPATH_R bdmsFbcTfpathR : rList) {
                    realPath = new RealPath();
                    if (bdmsFbcTfpathR.getYmdhm() != null && !"".equals(bdmsFbcTfpathR.getYmdhm())) {
                        realPath.setRealTime(bdmsFbcTfpathR.getYmdhm());
                        List<BDMS_FBC_TFPATH_F> bdmsFbcTfpathFs = fList.stream().filter
                                (f -> f.getRealtm().equals(bdmsFbcTfpathR.getYmdhm())).collect(Collectors.toList());
                        realPath.setPoints(fService.getTyphoonAll(bdmsFbcTfpathFs));
                    }
                    if (bdmsFbcTfpathR.getLttd() != null) {
                        realPath.setLttd(bdmsFbcTfpathR.getLttd());
                    }
                    if (bdmsFbcTfpathR.getLgtd() != null) {
                        realPath.setLgtd(bdmsFbcTfpathR.getLgtd());
                    }
                    if (bdmsFbcTfpathR.getMaxspeed() != null) {
                        realPath.setMaxSpeed(bdmsFbcTfpathR.getMaxspeed());
                    }
                    if (bdmsFbcTfpathR.getMoveway() != null) {
                        realPath.setMoveWay(bdmsFbcTfpathR.getMoveway());
                    }
                    if (bdmsFbcTfpathR.getMovespeed() != null) {
                        realPath.setMoveSpeed(bdmsFbcTfpathR.getMovespeed());
                    }
                    if (bdmsFbcTfpathR.getPressure() != null) {
                        realPath.setPressure(bdmsFbcTfpathR.getPressure());
                    }
                    if (bdmsFbcTfpathR.getRadius7() != null && !"".equals(bdmsFbcTfpathR.getRadius7())) {
                        realPath.setRadius7(bdmsFbcTfpathR.getRadius7().split("\\|"));
                    } else {
                        realPath.setRadius7(new String[]{});
                    }
                    if (bdmsFbcTfpathR.getRadius10() != null && !"".equals(bdmsFbcTfpathR.getRadius10())) {
                        realPath.setRadius10(bdmsFbcTfpathR.getRadius10().split("\\|"));
                    } else {
                        realPath.setRadius10(new String[]{});
                    }
                    if (bdmsFbcTfpathR.getRadius12() != null && !"".equals(bdmsFbcTfpathR.getRadius12())) {
                        realPath.setRadius12(bdmsFbcTfpathR.getRadius12().split("\\|"));
                    } else {
                        realPath.setRadius12(new String[]{});
                    }
                    realPaths.add(realPath);
                }
                jsonResult.setData(realPaths);
                jsonResult.setRemark("获取台风路径信息成功");
            }
        }
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }

    /**
     * 获取台风预报路径
     */
    @RequestMapping(value = "/GetForecastPaths", method = RequestMethod.POST)
    public JsonResult getForecastPaths(@RequestBody TyphoonParam param){
        JsonResult jsonResult = new JsonResult();
        if (param.getTyphoonId() != null && !"".equals(param.getTyphoonId()) &&
        param.getRealTime() != null && !"".equals(param.getRealTime())) {
            List<ForecastPath> forecastPaths = fService.getForecastPaths(param.getTyphoonId(),param.getRealTime());
            if (forecastPaths != null && forecastPaths.size() > 0) {
                jsonResult.setData(forecastPaths);
                jsonResult.setRemark("获取台风预报路径信息成功");
            }else {
                jsonResult.setRemark("获取台风预报路径信息失败，该台风id对应的预报路径信息为空");
            }
        }
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }

    /**
     * 根据年份获取台风的编号及名称
     */
    @RequestMapping(value = "/GetInfos", method = RequestMethod.POST)
    public JsonResult getInfos(@RequestBody GetInfosParam param){
        JsonResult jsonResult = new JsonResult();
        if (param.getYear() != null) {
            List<TyphoonInfo> typhoonInfos = bService.getInfos(param.getYear());
            if (param.getProjectId() != null && !"".equals(param.getProjectId())) {
                List<TyphoonInfo> typhoonInfos1 = new ArrayList<>();
                List<TyphoonCschemeInfo> infos = mService.getRealPaths(Integer.parseInt(param.getProjectId()));
                List<String> typhoonIds = new ArrayList<>();
                List<String> typhoonPs = new ArrayList<>();
                for (TyphoonCschemeInfo info : infos) {
                    if (info.getParams() != null && !"".equals(info.getParams())) {
                        String typhoonId = info.getParams().split(",")[0];
                        if (!typhoonIds.contains(typhoonId)) {
                            typhoonIds.add(typhoonId);
                        }
                        if ("y".equals(info.getFlag())) {
                            if (!typhoonPs.contains(typhoonId)) {
                                typhoonPs.add(typhoonId);
                            }
                        }
                    }
                }
                if (param.isFlag()) {
                    for (String typhoonId : typhoonPs) {
                        typhoonInfos1.addAll(typhoonInfos.stream().
                                filter(s -> s.getTyphoonId().equals(typhoonId)).collect(Collectors.toList()));
                    }
                }else {
                    for (String typhoonId : typhoonIds) {
                        typhoonInfos1.addAll(typhoonInfos.stream().
                                filter(s -> s.getTyphoonId().equals(typhoonId)).collect(Collectors.toList()));
                    }
                }
                if (typhoonInfos1.size() > 0) {
                    typhoonInfos1.sort(Comparator.comparing(TyphoonInfo::getTyphoonId).reversed());
                    jsonResult.setData(typhoonInfos1);
                    jsonResult.setRemark("获取该年台风信息成功");
                }else {
                    jsonResult.setRemark("获取该年台风信息失败，该年台风信息为空");
                }
            }else {
                if (typhoonInfos != null && typhoonInfos.size() > 0) {
                    jsonResult.setData(typhoonInfos);
                    jsonResult.setRemark("获取该年台风信息成功");
                }else {
                    jsonResult.setRemark("获取该年台风信息失败，该年台风信息为空");
                }
            }
        }else {
            jsonResult.setRemark("参数不完整");
        }
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }

    /**
     * 获取有台风的年份
     */
    @RequestMapping(value = "/GetYears", method = RequestMethod.POST)
    public JsonResult getYears(){
        JsonResult jsonResult = new JsonResult();
        List<String> years = bService.getYears();
        if (years != null && years.size() > 0) {
            jsonResult.setData(years);
            jsonResult.setRemark("获取有台风的年份信息成功");
        }else {
            jsonResult.setRemark("获取有台风的年份信息失败");
        }
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }

    /**
     * 获取正在发生的台风编号集合
     */
    @RequestMapping(value = "/GetOccurent", method = RequestMethod.POST)
    public JsonResult getOccurent(@RequestBody TyphoonParam param){
        JsonResult jsonResult = new JsonResult();
        List<TyphoonInfo> years = bService.getOccurent(param.getYear());
        if (years != null && years.size() > 0) {
            jsonResult.setData(years);
            jsonResult.setRemark("获取正在发生的台风信息成功");
        }else {
            jsonResult.setRemark("获取正在发生的台风信息失败，暂无正在发生的台风");
        }
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }
}
